#pragma once

class CSingleton
{
private:
    static CSingleton* m_lpInstance;
    static std::mutex m_Mutex;

private:
    CSingleton() { std::cout << "CSingleton" << std::endl; }
    virtual ~CSingleton() { std::cout << "~CSingleton" << std::endl; }

	CSingleton(const CSingleton&) {}
	CSingleton& operator=(const CSingleton&) {}

public:
    static CSingleton* GetInstance()
    {
        if (!m_lpInstance)
        {
            m_Mutex.lock();

            if (!m_lpInstance)
                m_lpInstance = new CSingleton;

            m_Mutex.unlock();
        }
        return m_lpInstance;
    }

private:
    class CGarb
    {
    public:
        virtual ~CGarb()
        {
            if (m_lpInstance)
            {
                std::cout << "~CGarb" << std::endl;
                delete m_lpInstance;
            }
        }
    };
    static CGarb m_Gc;
};

CSingleton* CSingleton::m_lpInstance = nullptr;
CSingleton::CGarb CSingleton::m_Gc;
std::mutex CSingleton::m_Mutex;

